package com.bjtech.web.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class InitServletContextListener implements ServletContextListener {
	protected final Log log = LogFactory.getLog(getClass());

	private static ApplicationContext ctx;

	public void contextInitialized(ServletContextEvent event) {
		ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());

		Constants.REALPATH = event.getServletContext().getRealPath("/");
		Constants.ContextPath = "/"	+ event.getServletContext().getServletContextName();

		log.info("init system REALPATH ===" + Constants.REALPATH);
		log.info("init preview ContextPath ==== " + Constants.ContextPath + "");

	}

	public static Object getBean(String name) {
		return ctx.getBean(name);
	}
	public static Object getBean(String name, Class clas) {
		return ctx.getBean(name, clas);
	}

	public void contextDestroyed(ServletContextEvent event) {
		ctx = null;
	}

}
